package cn.trigram.uuid;

import cn.trigram.uuid.bd.impl.CachedUidGenerator;
import cn.trigram.uuid.bd.worker.LocalWorkerIdAssigner;
import java.util.Objects;

public class BaiduUidUtil {

  private final UidConfigHelper helper = new UidConfigHelper(
      "snowflake");

  private static BaiduUidUtil instance;

  private final CachedUidGenerator generator;

  private BaiduUidUtil() {

    generator = new CachedUidGenerator(41, 10, 12,
        new LocalWorkerIdAssigner(helper.getInt("bd_worker_id", 0)), 60L);
  }

  public static BaiduUidUtil getInstance() {

    if (instance == null) {
      synchronized (BaiduUidUtil.class) {
        if (instance == null) {
          instance = new BaiduUidUtil();
        }
      }
    }
    return instance;
  }

  /**
   * 获取批次号 使用百度的id生成，也是基于snowflake
   */
  public long getUid() {

    return generator.getUID();
  }

  public String parseUid(Long uid) {

    Objects.requireNonNull(uid, "UID不能为空");
    return generator.parseUID(uid);
  }

}
